import { action, observable, computed } from 'mobx';
import { message } from 'antd';
import { WeaTableNew,WeaForm } from 'comsMobx';
import * as API from "../apis/contentExport";
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;

/**
 * 绩效-考核设置-考核指标项
 * jiansong 2019/6/11
 */
export  class ExportBlogListStore {

    @observable loading = true; //加载中
    @observable tableStore = new TableStore();
    @observable tableLogStore = new TableStore();
    @observable showShare = false; //是否展示共享范围对话框
    @observable data = []; //表单数据
    @observable shareData = []; //共享列表数据
    @observable formulaData = [];//自定义公式的值
    @observable enableData = [];//启用禁用更改数据
    @observable exportIds = "";
    @observable contents = "";
    @observable types = "";
    @observable isShowBatch = false;
    @observable exportLogListIsShow = false;
    @observable exportLogDetailIsShow = false;
    @observable detailRow = {exportIdspan:''};
    @observable
    searchAdForShareTable = {
      form: new WeaForm(),
      show: false,
      conditioninfo: []
    };
    @observable
    searchAdForShareLogTable = {
        formLog: new WeaForm(),
        showLog: false,
        conditioninfoLog: []
      };

    
  
    //指标类型
    @observable itemtype = {
        '1': getLabel('18090', '定性'),
        '2': getLabel('18089', '定量'),
    }
    @observable typeOption = [
        { key: "1", showname: getLabel('18090', '定性') },
        { key: "2", showname: getLabel('18089', '定量') }
    ];
   

    @observable
    shareBaseDataType = {
      form: new WeaForm(),
      showObj: false,
      objCondition: [],
    };

    @observable form = {
        operation: "add", //操作类型
        itemname: "", //指标名称
        itemdesc: "", //指标描述
        itemtype: "1", //指标类型
        itemunit: "", //数值单位
        formula: "1", //计算公式
        javaclass: "", //java类
        isvalid: "1", //是否启用
        kpiformula: "", //自定义公式的值
    };

    @observable itemid = ''//考核指标id

    setObjShow = (bool) => this.shareBaseDataType.showObj = bool;
    setIds = (ids) => this.exportIds = ids;
    isShowBatchDel = (selectd) => this.isShowBatch = selectd;
    setContents = (contents) => this.contents = contents;
    setTypes =  (types) => this.types = types;
    setExportLogListIsShow = (bool) => this.exportLogListIsShow = bool;
    setExportLogDetailIsShow = (bool,params) => {this.exportLogDetailIsShow = bool; this.detailRow= params};
    doInit = () => {
        this.searchAdForShareTable = {
          ...this.searchAdForShareTable,
          form: new WeaForm(),
          show: false,
        }
        this.searchAdForShareLogTable = {
            ...this.searchAdForShareLogTable,
            formLog: new WeaForm(),
            showLog: false,
          }
        this.getTableDatas();
        this.getTableLogDatas();
        this.getSearchCondition();
        this.getSearchLogCondition();
        //this.getShareBaseCreate();
      };

    @action("渲染table数据")
    getTableDatas = () => {
        let params =
        (this.searchAdForShareTable.form &&
          this.searchAdForShareTable.form.getFormParams()) ||
        {};
        this.loading = true;
        API.exportBlogList(params).then(action(res => {
            if (res.api_status) {
                this.tableStore.getDatas(res.datas, 1);
                this.enableData = [];
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

    @action("渲染操作日志数据")
    getTableLogDatas = () => {
        let params =
        (this.searchAdForShareLogTable.formLog &&
          this.searchAdForShareLogTable.formLog.getFormParams()) ||
        {};
        this.loading = true;
        API.exportBlogListLog(params).then(action(res => {
            if (res.api_status) {
                this.tableLogStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

    @action('改变tableStore中启用禁用数据')
    changeIsValid = (id) => {
        this.tableStore.datas && this.tableStore.datas.map(item => {
            if (item.id == id) {
                if (item.isvalid == '1') {
                    item.isvalid = 0;
                } else {
                    item.isvalid = 1;
                }
                if (this.enableData.includes(id + '_0') || this.enableData.includes(id + '_1')) {
                    this.enableData.splice(this.enableData.findIndex(v => v == id + "_0" || v == id + "_1"), 1)
                } else {
                    this.enableData.push(id + '_' + item.isvalid)
                }
            }
        })
    }

    
  // 表格高级搜索
  getSearchCondition = () => {
    API.getBlogBackstageExportSearchCondition().then(res => {
      this.searchAdForShareTable.form.initFormFields(res.conditioninfo);
      this.searchAdForShareTable.conditioninfo = res.conditioninfo;
    });
  };


   // 表格高级搜索
   getSearchLogCondition = () => {
    API.getBlogBackstageExportLogSearchCondition().then(res => {
      this.searchAdForShareLogTable.formLog.initFormFields(res.conditioninfo);
      this.searchAdForShareLogTable.conditioninfoLog = res.conditioninfo;
    });
  };

    // 指定共享添加分享规则
  getBlogBackstageExportAddCondition = params => {
    API.getBlogBackstageExportAddCondition(params).then(res => {
       this.shareBaseDataType.objCondition = res.conditioninfo;
    });
  };

  addExportPermissions = (params, reset = false) => {
    API.operateExportPermissions(params).then(res => {
      this.shareBaseDataType.showObj = false;
      if(res.status == 1){
        this.getTableDatas();
      }else {
        message.success(`${getLabel(30651, '操作失败')}`);
      }
    });
  };

    @action("表单关闭的时候清空表单内容")
    closeForm = () => {
        this.initForm();
        this.changeShowUpdataDialog();
    };

    @action("搜索条件改变")
    onFormChange = (params) => {
        for (let key in params) {
            this.form[key] = params[key];
        }
    }

    @action("初始化添加表单")
    ininAddForm = () => {
        this.changeShowUpdataDialog();
    };

    @action('显示共享范围dialog')
    showShowShare = (id) => {
        this.itemid = id;
        if (this.itemid) {
            //获取考核指标项的共享范围列表
            this.getShareList();
        }
        this.showShare = true;
    }

    @action("关闭共享范围dialog")
    closeShowShare = () => {
        this.shareData = {};
        this.showShare = false;
    }

    @action("获取考核指标项共享列表")
    getShareList = () => {
        API.accessItemShareListApi({ itemid: this.itemid }).then(action(res => {
            if (res.api_status) {
                this.shareData = res.datas;
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        }));
    }



    @action("初始化表单内容")
    initForm = () => {
        this.form = {
            operation: "add",
            itemname: "", //指标名称
            itemdesc: "", //指标描述
            itemtype: "1", //指标类型
            itemunit: "", //数值单位
            formula: "1", //计算公式
            javaclass: "", //java类
            isvalid: "1", //是否启用
            kpiformula: "", //自定义公式的值
        };
        this.formulaData = [];
    };

    @action('保存考核指标启用禁用')
    saveEnable = () => {
        let params = {
            'operation': 'enable',
            'ids': this.enableData,
        }
        API.accessItemOperationApi(params).then(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error(getLabel('2012', '对不起，您暂时没有权限！'));
                } else {
                    message.success(getLabel('30700', '操作成功'));
                    this.getTableDatas()
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        })
    }

    @action("删除")
    deleteRow = (params) => {
        API.operateExportPermissions(params).then(res => {
            if(res.status == 1){
              this.getTableDatas();
              this.isShowBatch=false;
            }else {
              message.success(`${getLabel(30651, '操作失败')}`);
            }
          });
    };

    @action("删除")
    batchDel = () => {
        let params = {id:this.exportIds,contents:this.contents,types:this.types,operation:'delete'}
        API.operateExportPermissions(params).then(res => {
            if(res.status == 1){
              this.getTableDatas();
              this.isShowBatch=false;
            }else {
              message.success(`${getLabel(30651, '操作失败')}`);
            }
          });
    };

    @action("考核指标项-操作")
    operation = () => {
        this.loading = true;
        let params = { ...this.form };
        if (params.formula == '12') {
            params.kpiformula = (params.kpiformula).replace(getLabel('18087', '目标值'), "gval").replace(getLabel('508618', '完成值'), "cval");
        };

        API.accessItemOperationApi(params).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error(getLabel('2012', '对不起，您暂时没有权限！'));
                } else {
                    message.success(getLabel('30700', '操作成功'));
                    this.changeShowUpdataDialog()
                    this.getTableDatas()
                    this.initForm()
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.formulaData = [];
            this.form.kpiformula = '';
            this.loading = false;
        }));
    };

    @action("考核指标项-共享范围-操作")
    operationShare = (data) => {
        API.accessItemShareOperationApi({
            ...data,
            itemid: this.itemid,
            type: 'add'
        }).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error(getLabel('2012', '对不起，您暂时没有权限！'));
                } else {
                    this.getShareList();
                    message.success(getLabel('30700', '操作成功'));
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        }));
    };

    @action("考核指标项-共享范围-删除")
    delShare = (params) => {
        params.type = "del";
        API.accessItemShareOperationApi(params).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error(getLabel('2012', '对不起，您暂时没有权限！'));
                } else {
                    message.success(getLabel('30700', '操作成功'));
                    this.getShareList();
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        }));
    }

}